﻿using System;
using System.Data.SqlClient;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace PowerMon
{
    internal class Threadtcpserver
    {
        private Socket mainsocket;

        public Threadtcpserver()
        {
            Thread tc = new Thread(new ThreadStart(ThreadToReceive));
            tc.Start();
        }

        //public static int conns = 0;
        public static int receives = 0;

        public void ThreadToReceive()
        {


            string receivedata;
            string clip;

            int recv;
            byte[] data = new byte[1024];

            //得到本机IP，设置TCP端口号
            IPEndPoint ip = new IPEndPoint(IPAddress.Any, 12345);
            Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            //绑定网络地址
            newsock.Bind(ip);
            IPEndPoint senderip = new IPEndPoint(IPAddress.Any, 0);
            EndPoint Remote = (EndPoint)(senderip);
            //recv = newsock.ReceiveFrom(data, ref Remote);
            while (true)
            {
                data = new byte[1024];
                //发送接收信息
                recv = newsock.ReceiveFrom(data, ref Remote);
                // Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

                //textipmes = textipmes + Encoding.ASCII.GetString(data, 0, recv) + " " + Remote.ToString() + "\n";

                receivedata = Encoding.ASCII.GetString(data, 0, recv);
                clip = Remote.ToString();

                string[] arg = new string[10];
                string[] strGPS;
                strGPS = receivedata.Split(',');
                arg[0] = (string)strGPS[0];
                arg[1] = (string)strGPS[1];


                arg[2] = (string)strGPS[2];
                arg[3] = (string)strGPS[3];
                arg[4] = (string)strGPS[4];


                arg[5] = (string)strGPS[5];
                arg[6] = (string)strGPS[6];
                arg[7] = (string)strGPS[7];

                arg[8] = (string)strGPS[8];
                

                string[] iptemp;
                iptemp = clip.Split(':');
                string ipfin = iptemp[0];
                if (arg[0] == "hua" )
                {
                    receives++;
                    int tempo = Convert.ToInt32(float.Parse(arg[2]) - float.Parse(arg[3]));

                    Thread threadtosql = new Thread(new ThreadStart(delegate
                    {
                        SqlHelperParas.ExecutenonQuery("insert into T_Monitor (Device,TempI,TempO,GPSL,GPSW,TempD,DTime) values (@Device,@TempH,@TempL,@GPSJ,@GPSW,@TempD ,'" + DateTime.Now.ToString() + "' )", new SqlParameter("@Device", arg[1]), new SqlParameter("@TempH", arg[2]), new SqlParameter("@TempL", arg[3]), new SqlParameter("@GPSJ", arg[4]), new SqlParameter("@GPSW", arg[6]), new SqlParameter("@TempD", tempo));
                        SqlHelperParas.ExecutenonQuery("Update T_DeviceIP Set IP=@IP where Device=@Device", new SqlParameter("@Device", arg[1]), new SqlParameter("@IP", ipfin));

                    }));


                    threadtosql.Start();



                }

                newsock.SendTo(data, recv, SocketFlags.None, Remote);
            }
        }

  

        public static string GetLocalIP()
        {
            try
            {
                string HostName = Dns.GetHostName(); //得到主机名
                IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
                for (int i = 0; i < IpEntry.AddressList.Length; i++)
                {
                    //从IP地址列表中筛选出IPv4类型的IP地址
                    //AddressFamily.InterNetwork表示此IP为IPv4,
                    //AddressFamily.InterNetworkV6表示此地址为IPv6类型
                    if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
                    {
                        return IpEntry.AddressList[i].ToString();
                    }
                }
                return "127.0.0.1";
            }
            catch (Exception ex)
            {
                return "127.0.0.1";
            }
        }
    }
}